package com.study.lession.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.study.lession.domain.LessionTeacherInfo;
import com.study.lession.domain.LessionUserInfo;
import com.study.lession.service.LessionTeacherInfoService;
import com.study.lession.service.LessionUserInfoService;

/**
 * 通讯录控制器
 * @author warm
 * @version 2019年07月11日
 */
@Controller
public class ContactController {
	
	@Autowired
	private LessionUserInfoService contactService;
	@Autowired
	private LessionTeacherInfoService contactServiceteacher;
	/**
	 * 登录
	 * @param lessionUserInfo
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(HttpServletRequest request, LessionUserInfo lessionUserInfo) {
		
		List<LessionUserInfo> list = contactService.queryLessionUserInfoList(lessionUserInfo);
		
		ModelAndView view = new ModelAndView();
		view.addObject("list", list);
		view.setViewName("contact/list");
		return view;
	}
	
	/**
	 * 修改操作
	 * @param lessionUserInfo
	 * @return
	 */
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	public ModelAndView edit(HttpServletRequest request, LessionUserInfo lessionUserInfo) {
		
		int count = contactService.editContact(lessionUserInfo);
		
		ModelAndView view = new ModelAndView();
		if (count == 1) {
			view.setViewName("contact/list");
		} else {
			view.addObject("msg", "修改失败！");
			view.setViewName("contact/edit");
		}
		return view;
	}
	
	/**
	 * 修改页面
	 * @param lessionUserInfo
	 * @return
	 */
	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public ModelAndView edit(HttpServletRequest request, Long id) {
		
		LessionUserInfo lessionUserInfo = contactService.viewContact(id);
		
		ModelAndView view = new ModelAndView();
		view.addObject("lessionUserInfo", lessionUserInfo);
		view.setViewName("contact/edit");
		return view;
	}
	

	/**
	 * 详情页面
	 * @param lessionUserInfo
	 * @return
	 */
	@RequestMapping(value = "/view", method = RequestMethod.GET)
	public ModelAndView view(HttpServletRequest request, Long id) {
		
		LessionUserInfo lessionUserInfo = contactService.viewContact(id);
		
		ModelAndView view = new ModelAndView();
		view.addObject("lessionUserInfo", lessionUserInfo);
		view.setViewName("contact/view");
		return view;
	}
	
	/**
	 * 删除
	 * @param lessionUserInfo
	 * @return
	 */
	@RequestMapping("/delete")
	public ModelAndView delete(HttpServletRequest request, Long id) {
		
		int count = contactService.delContact(id);
		
		ModelAndView view = new ModelAndView();
		if (count == 1) {
			view.setViewName("redirect:logout.action");
		} else {
			view.addObject("msg", "删除失败！");
			view.setViewName("contact/list");
		}
		return view;
	}
	
	
	
	/**
	 * 登录
	 * @param lessionUserInfo
	 * @return
	 */
	@RequestMapping("/listteacher")
	public ModelAndView listteacher(HttpServletRequest request, LessionTeacherInfo lessionTeacherInfo) {
		
		List<LessionTeacherInfo> list = contactServiceteacher.queryLessionTeacherInfoList(lessionTeacherInfo);
		
		ModelAndView view = new ModelAndView();
		view.addObject("listteacher", list);
		view.setViewName("contact/listteacher");
		return view;
	}
	
	/**
	 * 修改操作
	 * @param lessionUserInfo
	 * @return
	 */
	@RequestMapping(value = "/editteacher", method = RequestMethod.POST)
	public ModelAndView editteacher(HttpServletRequest request, LessionTeacherInfo lessionTeacherInfo) {
		
		int count = contactServiceteacher.editContact(lessionTeacherInfo);
		
		ModelAndView view = new ModelAndView();
		if (count == 1) {
			view.setViewName("contact/listteacher");
		} else {
			view.addObject("msg", "修改失败！");
			view.setViewName("contact/editteacher");
		}
		return view;
	}
	
	/**
	 * 修改页面
	 * @param lessionUserInfo
	 * @return
	 */
	@RequestMapping(value = "/editteacher", method = RequestMethod.GET)
	public ModelAndView editteacher(HttpServletRequest request, Long id) {
		
		LessionTeacherInfo lessionTeacherInfo = contactServiceteacher.viewContact(id);
		
		ModelAndView view = new ModelAndView();
		view.addObject("lessionTeacherInfo", lessionTeacherInfo);
		view.setViewName("contact/editteacher");
		return view;
	}
	

	/**
	 * 详情页面
	 * @param lessionUserInfo
	 * @return
	 */
	@RequestMapping(value = "/viewteacher", method = RequestMethod.GET)
	public ModelAndView viewteacher(HttpServletRequest request, Long id) {
		
		LessionTeacherInfo lessionTeacherInfo = contactServiceteacher.viewContact(id);
		
		ModelAndView view = new ModelAndView();
		view.addObject("lessionTeacherInfo", lessionTeacherInfo);
		view.setViewName("contact/viewteacher");
		return view;
	}
	
	/**
	 * 删除
	 * @param lessionUserInfo
	 * @return
	 */
	@RequestMapping("/deleteteacher")
	public ModelAndView deleteteacher(HttpServletRequest request, Long id) {
		
		int count = contactServiceteacher.delContact(id);
		
		ModelAndView view = new ModelAndView();
		if (count == 1) {
			view.setViewName("redirect:logoutteacher.action");
		} else {
			view.addObject("msg", "删除失败！");
			view.setViewName("contact/listteacher");
		}
		return view;
	}
}
